#import "SearchView.h"
#import "Shares.h"
#import "SolarSeekApplication.h"

@implementation SearchView

- (void)copyString:(NSString *)string {
	NSPasteboard *pb = [NSPasteboard generalPasteboard];
	[pb addTypes:[NSArray arrayWithObjects:NSStringPboardType,nil] owner:self];
	[pb setString:string forType:NSStringPboardType];
}

- (IBAction)copyShareNames:(id)sender {
	NSArray *a = [self sharesSelected];
	NSMutableString *string = [[NSMutableString alloc] init];
	Share *s;
	BOOL first = YES;
	NSEnumerator *e = [a objectEnumerator];
	while(s = [e nextObject]) {
		if(first)
			first = NO;
		else
			[string appendString:@", "];
		[string appendString:[s filenameShared]];
	}
	[self copyString:string];
	[string autorelease];
}


- (NSRect)contentView {
	NSScrollView *scrollView = [nibTableResults enclosingScrollView];       
	NSClipView *clipView = [scrollView contentView];
	NSRect visibleRect = [clipView documentVisibleRect];
	return visibleRect;
}

- (void)setContentView:(NSRect)view {
	NSScrollView *scrollView = [nibTableResults enclosingScrollView];       
	NSClipView *clipView = [scrollView contentView];
	[clipView scrollToPoint:view.origin];
}

- (NSArray *)participantsSelected { 
	NSMutableSet *set = [NSMutableSet set];
	NSEnumerator *e = [[nibControllerShares selectedObjects] objectEnumerator];
	SharePeer *s;
	while(s = [e nextObject])
		[set addObject:[s peer]];
	return [set allObjects];
}

- (NSArray *)sharesSelected {
	NSView *v = [[NSApp selectedSourceListElement] view];
	if(v != self && [v respondsToSelector:@selector(sharesSelected)])
		return [v performSelector:@selector(sharesSelected)];
	else
		return [nibControllerShares selectedObjects];
}

- (void)setSharesSelected:(NSArray *)shares {
	[nibControllerShares setSelectedObjects:shares];
}

@end
